#coding=utf-8
#!/usr/bin/python

import threading 
import time

con = threading.Condition()


class some(threading.Thread):
    def __init__(self,name):
        self.a = threading.Thread.__init__(self,name= name)
    def run (self):
        global x
        con.acquire()
        if x >0 :
            print 'some do'
            while x > 0:
                x -= 1
                print 'x :'+ str(x)
                time.sleep(0.3)
            con.notify()
        else:
            print 'wait....'
            con.wait()          
        con.release()
        
class other(threading.Thread):
    def __init__(self,name):
        threading.Thread.__init__(self,name =name)
    def run (self):
        global x 
        con.acquire()
        if x <= 0 :
            x = 5
            print 'initialized x ...'
            time.sleep(0.3)
            print 'this is a test...'
            time.sleep(0.4)
            print 'initialized ... ok !'
            time.sleep(0.3)
            print '- - ReStart - -'
            time.sleep(0.4)
            print 'x : '+str(x)
            con.notify()
        else:
            print 'other wait...'
            con.wait()
        con.release()

x= 5

def test():
    s = some('1')
    o = other('2')    
    s.start()
    o.start()
    count = 0
    while count < 5:
        s.run()
        o.run()
        count += 1
test()
